var e = require("../../api.js"),
  n = getApp(),
  t = getApp();

Page({
  data: {
    contact_tel: "",
    show_customer_service: 0,
    showDialog: true,
    all_color: ''
  },
  onLoad: function(e) {
    t.pageOnLoad(this, e);
    this.themeColor();
  },
  themeColor: function () {
    var cl = wx.getStorageSync('_navigation_bar_color').themeColor;
    var cl_b = !cl || cl == '#ffffff' ? '#ff4544' : cl;
    var color_rgb = this.hexToRgb(cl_b);
    var back_g = 'rgba(' + color_rgb.r + ',' + color_rgb.g + ',' + color_rgb.b + ',.1)'
    this.setData({
      all_color: cl ? cl : '#ffffff',
      back_g: back_g
      // back_end: back_end
    });
    wx.setNavigationBarColor({
      frontColor: cl != '#ffffff' ? '#ffffff' : '#000000',
      backgroundColor: cl_b,
      animation: {
        duration: 0,
        timingFunc: 'easeIn'
      }
    })
  },
  hexToRgb: function (hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
      r: parseInt(result[1], 16),
      g: parseInt(result[2], 16),
      b: parseInt(result[3], 16)
    } : null;
  },
  loadData: function(a) {
    var n = this;
    if (!(n.data.user_info && n.data.user_info.id > 0)) {
        wx.showLoading({
          title: '加载中',
        })
    }
    n.setData({
      store: wx.getStorageSync("store")
    }), t.request({
      url: e.user.index,
      data:{is_new:1},
      success: function(e) {
        e.data.take_code = (e.data.take_code + '').split('');
        0 == e.code && ("my" == n.data.__platform && e.data.menus.forEach(function(t, a, n) {
            "bangding" === t.id && e.data.menus.splice(a, 1, 0);
          }), n.setData(e.data), wx.setStorageSync("pages_user_user", e.data), wx.setStorageSync("share_setting", e.data.share_setting),
          wx.setStorageSync("user_info", e.data.user_info));
      },
      complete: function (e) {
        wx.hideLoading();
      }
    });
  },
  onReady: function() {},
  onShow: function() {
    //wx.hideTabBar()
    t.pageOnShow(this), this.loadData();
    this.show_navs();
    this.cart_num();
  },
  show_navs: function() {
    var that = this;
    var nav = wx.getStorageSync("_navbar");
    var url = "/pages/user/user";
    for (var i = 0; i < nav.navs.length; i++) {
      if (nav.navs[i].url == url) {
        if (getApp().globalData.nav_index == i) {
          console.log(typeof that.getTabBar);
          if (typeof that.getTabBar === 'function' &&
            that.getTabBar()) {
            that.getTabBar().setData({
              selected: getApp().globalData.nav_index,
              is_show: 1
            })
          }
        } else {
          getApp().globalData.nav_index = i;
          if (typeof that.getTabBar === 'function' &&
            that.getTabBar()) {
            that.getTabBar().setData({
              selected: getApp().globalData.nav_index,
              is_show: 1
            })
          }
        }
      }
    }
  },
  callTel: function(e) {
    var t = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: t
    });
  },
  apply: function(a) {
    var n = wx.getStorageSync("share_setting"),
      i = wx.getStorageSync("user_info");
    1 == n.share_condition ? wx.navigateTo({
      url: "/pages/add-share/index"
    }) : 0 != n.share_condition && 2 != n.share_condition || (0 == i.is_distributor ? wx.showModal({
      title: "申请成为分销商",
      content: "是否申请？",
      success: function(o) {
        o.confirm && (wx.showLoading({
          title: "正在加载",
          mask: !0
        }), t.request({
          url: e.share.join,
          method: "POST",
          data: {
            form_id: a.detail.formId
          },
          success: function(e) {
            0 == e.code && (0 == n.share_condition ? (i.is_distributor = 2, wx.navigateTo({
              url: "/pages/add-share/index"
            })) : (i.is_distributor = 1, wx.navigateTo({
              url: "/pages/share/index"
            })), wx.setStorageSync("user_info", i));
          },
          complete: function() {
            wx.hideLoading();
          }
        }));
      }
    }) : wx.navigateTo({
      url: "/pages/add-share/index"
    }));
  },
  verify: function(e) {
    wx.scanCode({
      onlyFromCamera: !1,
      success: function(e) {
        wx.navigateTo({
          url: "/" + e.path
        });
      },
      fail: function(e) {
        wx.showToast({
          title: "失败"
        });
      }
    });
  },
  extract: function() {
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  member: function() {
    wx.navigateTo({
      url: "/pages/member/member"
    });
  },
  integral_mall: function(e) {
    t.permission_list && t.permission_list.length && function(e, t) {
      return -1 != ("," + e.join(",") + ",").indexOf("," + t + ",");
    }(t.permission_list, "integralmall") && wx.navigateTo({
      url: "/pages/integral-mall/index/index"
    });
  },
  saoma: function() {
    wx.scanCode({
      success(res) {
        wx.navigateTo({
          url: '../../' + res.path

        })
      }
    })
  },
  // 获取购物车数量
  cart_num: function () {
    var that = this;
    wx.getStorage({
      key: 'user_info',
      success: function (res) {
        t.request({
          url: e.cart.list,
          success: function (t) {
            if (0 == t.code) {
              if (typeof that.getTabBar === 'function' &&
                that.getTabBar()) {
                if (t.data.mch_list.length > 0) {
                  var length = t.data.list.length + t.data.mch_list[0].list.length;
                } else {
                  var length = t.data.list.length
                }
                that.getTabBar().setData({
                  cart_num: length
                })
              }
            }
          }
        });
      },
    })  
  },
  mobileUpdate:function () {
    var setting = this.data.setting;
    setting.manual_mobile_auth = !setting.manual_mobile_auth;
    this.setData({
      setting:setting
    })
  },
  getPhoneNumber: function (t) {
    var a = this;
    "getPhoneNumber:fail user deny" == t.detail.errMsg ? wx.showModal({
      title: "提示",
      showCancel: !1,
      content: "未授权",
      success: function (n) { }
    }) : (wx.showLoading({
      title: "授权中"
    }), wx.login({
      success: function (i) {
        if (i.code) {
          var o = i.code;
          n.request({
            url: e.user.user_binding,
            method: "POST",
            data: {
              iv: t.detail.iv,
              encryptedData: t.detail.encryptedData,
              code: o
            },
            success: function (n) {
              if (0 == n.code) {
                var e = a.data.user_info;
                e.binding = n.data.dataObj, wx.setStorageSync("__user_info", e), a.setData({
                  user_info: e,
                });
              } else wx.showToast({
                title: "授权失败,请重试",
                icon: 'none'
              });
            },
            complete: function (n) {
              wx.hideLoading();
            }
          });
        } else wx.showToast({
          title: "获取用户登录态失败！" + i.errMsg,
          image: "/images/icon-warning.png"
        });
      }
    }));
  },
  getLoginUser: function () {
    this.setData({
      user_info_show: !0
    });
  },
//   onChooseAvatar(e3) {
//     //   console.log(e, 'eeee')
//     var avatarUrl = e3.detail.avatarUrl
//     wx.showLoading({
//       title: '提交中',
//     })
//     if (avatarUrl) {
//         // 更新用户信息
//         getApp().request({
//             url: e.user.update_user,
//             method: "POST",
//             data: {
//                 avatarUrl: avatarUrl
//             },
//             success: function (n) {
//                 wx.reLaunch({
//                   url: '/pages/user/user',
//                 })
//             },
//             complete: function (n) {
//                 wx.hideLoading();
//             }
//         });
//     }
//   }
});